package com.daiyang.seckill.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.daiyang.seckill.mapper.UserMapper;
import com.daiyang.seckill.pojo.User;
import com.daiyang.seckill.service.UserService;
import com.daiyang.seckill.util.MD5Util;
import com.daiyang.seckill.vo.LoginVo;
import com.daiyang.seckill.vo.RespBean;
import com.daiyang.seckill.vo.RespBeanEnum;
import com.daiyang.seckill.vo.ValidatorUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        // 判断是否为空，参数校验
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        //手机校验
        if(!ValidatorUtil.isMobile(mobile)){
            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
        }

        //查询数据库
        User user =userMapper.selectById(mobile);
        if(user==null){
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        if(!MD5Util.midPassToDBPass(password,user.getSlat())
                .equals(user.getPassword())) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        return RespBean.success();
    }
}
